﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace DomainModel.Entities
{
    public class User
    {
        
        public virtual int UserID{ get; set; }

        [Required(ErrorMessage = "Name is required.")]
        [StringLength(200, ErrorMessage = "Name must be 200 characters or less.")]
        public virtual string Name { get; set; }

        public virtual string RealName { get; set; }

        [Required(ErrorMessage = "Email is required.")]
        [StringLength(200, ErrorMessage = "Email must be 200 characters or less.")]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Valid Email Address is required.")]
        public virtual string Email { get; set; }

        public virtual string OpenID { get; set; }

        [Required(ErrorMessage = "About is required.")]
        [StringLength(1000, ErrorMessage = "Content must be 1000 characters or less.")]
        public virtual string About {get;set;}

        public virtual string UserRole { get; set; }

        public virtual int LoginTimes { get; set; }
        public virtual DateTime DateLastLogin { get; set; }
        public virtual DateTime DateUpdated { get; set; }

        public virtual string ImageName { get; set; }
        
        # region Logic

        /// <summary>
        /// Exports a short string list of Id, Email, Name separated by |
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Join(",", new string[] { this.UserID.ToString(), this.UserRole.ToString()});
        }

        /// <summary>
        /// Imports Id and role from a | separated string
        /// </summary>
        /// <param name="itemString"></param>
        public bool FromString(string itemString)
        {
            if (string.IsNullOrEmpty(itemString))
                return false;

            string[] strings = itemString.Split(',');
            if (strings.Length < 2)
                return false;

            this.UserID = int.Parse(strings[0]);
            this.UserRole = strings[1];

            return true;
        }

        #endregion


    }
}
